package org.myminidao.session;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import org.myminidao.dao.DaoCreate;

public class DefaultSession implements MyMiniDaoSession {
	
	private Connection connection;
	
	@SuppressWarnings("rawtypes")
	private DaoCreate daoCreate;

	public void close() throws IOException {
		if (null != connection) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public <T> T getDao(Class<T> type) {
		daoCreate = new DaoCreate<T>();
		daoCreate.setDaoInterface(type);
		return (T) daoCreate.createDao(connection);
	}

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}
}
